<?php

class DBManager extends SQLite3 {

    function __construct($dbPath) {
        $this->open($dbPath);
    }


    public function writeToTable($dateCreate, $content) {

        $sql = <<<BBB
            replace into jypd (dateCreate,content)
            values ("$dateCreate", "$content")
        BBB;

        $this->exec($sql);

    }


    public function appendContent($dateCreate, $content) {

        $originalContent = $this->readFromTable($dateCreate);

        if (!empty($originalContent)) {
            $newContent = $originalContent . PHP_EOL . PHP_EOL . $content;
            $this->writeToTable($dateCreate, $newContent);
        } else {
            $this->writeToTable($dateCreate, $content);
        }

    }


    public function readFromTable($dateCreate) {

        $sql = <<<BBB
            select content from jypd
            where dateCreate = "$dateCreate"
        BBB;

        return $this->querySingle($sql);

    }


    public function deleteContent($dateCreate) {
        $sql = <<<BBB
            delete from jypd
            where dateCreate = "$dateCreate"
        BBB;
        $this->exec($sql);
    }


    // 创建数据库
    public function createTable() {

        $sql = <<<BBB
            CREATE TABLE "jypd" (
                id         INTEGER PRIMARY KEY
                                   UNIQUE,
                content    TEXT,
                dateCreate TEXT    NOT NULL UNIQUE
            );
        BBB;

        $this->exec($sql);

    }


    // 删除表
    public function deleteTable() {

        $sql = <<<BBB
            drop table if exists jypd
        BBB;

        $this->exec($sql);

    }


    //重建数据库
    public function reCreateDatabase() {
        $this->deleteTable();
        $this->createTable();
    }

}